import os

from PyQt4 import QtCore, QtGui

from main_ui import MainWindow

class MainLogic(MainWindow):
  def __init__(self):
    MainWindow.__init__(self)

    self.new_db_btn.clicked.connect(self.new_db)
    self.load_db_btn.clicked.connect(self.load_db)

    self.db_view.itemChanged.connect(self.db_item_changed)
    self.db_view.itemClicked.connect(self.db_item_selected)
    self.db_view.setIconSize(QtCore.QSize(30, 30))
    
    self.toggle_img_view_btn.clicked.connect(self.toggle_img_view)
    self.add_img_btn.clicked.connect(self.add_image)
    self.add_img_folder_btn.clicked.connect(self.add_image_folder)

    self.img_view.setIconSize(QtCore.QSize(60, 60))

    self.default_db_dir = ''
    self.default_db_name = 'New Database'
    self.default_db_icon = QtGui.QIcon('default.jpg')

    self.default_img_dir = '' 
    self.img_model = ''
    self.img_files = []

  def __add_image(self, file):
    print(file)
    self.img_files.append(file)

    # add to current db, and get a img_id
    img = QtGui.QIcon(file)
    filename = file.split(os.sep)[-1]
    item = QtGui.QListWidgetItem(img, filename)
    self.img_view.addItem(item)
    QtCore.QCoreApplication.processEvents()
    #item.setData(img_id & filepath)

  def add_image(self):
    print('add images')
    options = QtGui.QFileDialog.Options()
    files = QtGui.QFileDialog.getOpenFileNames(self, 
        'Select image files', self.default_img_dir,
        'All Files (*);;Text Files (*.txt)', options)
    if files:
      self.default_img_dir = files[0]
      for file in files:
        self.__add_image(file)
  
  def add_image_folder(self):
    print('add image folder')
    options = (QtGui.QFileDialog.DontResolveSymlinks | 
               QtGui.QFileDialog.ShowDirsOnly)
    directory = QtGui.QFileDialog.getExistingDirectory(self,
        'Create database from directory', self.default_img_dir, options)
    if directory:
      self.default_img_dir = directory
      for filename in os.listdir(directory):
        file = os.path.join(directory, filename);
        self.__add_image(file)

  def toggle_img_view(self):
    self.img_view.toggle_view()

  def db_item_changed(self, item):
    print(item.text())

  def db_item_selected(self, item):
    db_id = item.data(QtCore.Qt.UserRole)
    print('db id: %d, db name: %s' % (db_id, item.text()))
    # fetch db data, and display

  def new_db(self):
    print('add database')
    # create a new db, and get db id
    db_id = 123
    item = QtGui.QListWidgetItem(self.default_db_icon, self.default_db_name, 
                                 self.db_view) 
    item.setFlags(item.flags() | QtCore.Qt.ItemIsEditable)
    item.setData(QtCore.Qt.UserRole, db_id)
    #self.dbView.addItem(item)

  def create_db(self):
    print('create database')
    options = (QtGui.QFileDialog.DontResolveSymlinks | 
               QtGui.QFileDialog.ShowDirsOnly)
    directory = QtGui.QFileDialog.getExistingDirectory(
        self, 'Select a directory containing image files', 
        self.default_db_dir, options)
    if directory:
      self.default_db_dir = directory
      #load files

  def load_db(self):
    print('load database')
    options = QtGui.QFileDialog.Options()
    files = QtGui.QFileDialog.getOpenFileNames(self, 
        'Select database files', self.default_db_dir,
        'All Files (*);;Text Files (*.txt)', options)
    if files:
      self.default_db_dir = files[0]
      # pass these filenames to db manager to parse and get db information
      for filename in files:
        print(filename)


if __name__ == '__main__':
  import sys
  app = QtGui.QApplication(sys.argv)

  window = MainLogic()
  window.resize(800, 600)
  window.show()

  sys.exit(app.exec_())
